package com.cs.common.plugin;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;

import javax.servlet.ServletException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.xml.sax.SAXException;

import com.cs.common.init.InitLoadCollection;
import com.cs.common.init.beans.InitLoadBean;
import com.cs.common.xml.XMLBaseDAO;

/**
 * Class for execute when the application is deployed
 * @version 1.0 
 * @date  2008-10-15
 * @author jdelgado
 */

public class InitPlugIn implements PlugIn {
	/** Init Method in all initial load */
	private static final String INIT_METHOD = "init";
	/** Init load config  */
	
	/**
	 * <p></p>
	 * @since January 08 2009
	 * @author Luis Fernando Celis Romero
	 */
	public static String LOCALPATH = null;
	/**
	 * <p></p>
	 * @since January 08 2009
	 * @author Luis Fernando Celis Romero
	 */
	public final static String CONTEXT_ROOT = "/";
	
	private String initLoadConfig;
	/** Init load rules  */
	private String initLoadRules;
	/**
	 * Gets init load config path
	 * @return <String> init load rules
	 */
	public String getInitLoadConfig() {
		return initLoadConfig;
	}
	/**
	 * Sets init load config path
	 * @param init load config path
	 */
	public void setInitLoadConfig(String initLoadConfig) {
		this.initLoadConfig = initLoadConfig;
	}
	/**
	 * Gets init load rules path
	 * @return <String> init load rules path
	 */
	public String getInitLoadRules() {
		return initLoadRules;
	}
	/**
	 * Sets init load rules path
	 * @param initLoadRules init load rules path
	 */
	public void setInitLoadRules(String initLoadRules) {
		this.initLoadRules = initLoadRules;
	}
	/**
	 * Free memory and conections opened
	 */
	public void destroy() {
		
	}
	/**
	 * Whe the application start, run this method
	 */
	public void init(ActionServlet actionServlet, ModuleConfig moduleConfig)throws ServletException {
		
		LOCALPATH = actionServlet.getServletContext().getRealPath("/");
		java.io.File file = new java.io.File(LOCALPATH);
		
		LOCALPATH = file.toURI().toString();
		System.out.println(LOCALPATH);
		
		String initLoadRules = null;
		String initLoadConfig = null;
		
		InitLoadCollection initLoadCollection = null;
		Iterator iteratorInitLoads = null;
		InitLoadBean initLoadBean = null;
		
		Object initObject = null;
		Method method = null;
		Class parameters[] = null;
		Object values [] = null;
		try {
			parameters = new Class[1];
			values = new Object[1];
			parameters[0] = Object.class;
			initLoadCollection = new InitLoadCollection();
			initLoadRules = getInitLoadRules();
			initLoadConfig = getInitLoadConfig();
			XMLBaseDAO.parse(initLoadConfig, initLoadRules, initLoadCollection);
			iteratorInitLoads = initLoadCollection.iterator();
			while( iteratorInitLoads.hasNext() ){
				initLoadBean = (InitLoadBean)iteratorInitLoads.next();
				initObject = Class.forName(initLoadBean.getClassName()).newInstance();
				XMLBaseDAO.parse(initLoadBean.getXmlData(), initLoadBean.getXmlRules(), initObject);
				values[0] = initObject;
				method = initObject.getClass().getMethod(INIT_METHOD,  parameters);
				method.invoke(initObject, values);
			}
		} catch (IOException iOException) {
			iOException.printStackTrace(System.out);
			throw new ServletException(iOException);
		} catch (SAXException sAXException) {
			sAXException.printStackTrace(System.out);
			throw new ServletException(sAXException); 
		} catch (ClassNotFoundException classNotFoundException) {
			classNotFoundException.printStackTrace(System.out);
			throw new ServletException(classNotFoundException);
		} catch (SecurityException securityException) {
			securityException.printStackTrace(System.out);
			throw new ServletException(securityException);
		} catch (NoSuchMethodException noSuchMethodException) {
			noSuchMethodException.printStackTrace(System.out);
			throw new ServletException(noSuchMethodException);
		} catch (IllegalArgumentException illegalArgumentException) {
			illegalArgumentException.printStackTrace(System.out);
			throw new ServletException(illegalArgumentException);
		} catch (IllegalAccessException illegalAccessException) {
			illegalAccessException.printStackTrace(System.out);
			throw new ServletException(illegalAccessException);
		} catch (InstantiationException instantiationException) {
			instantiationException.printStackTrace(System.out);
			throw new ServletException(instantiationException);
		} catch (InvocationTargetException invocationTargetException) {
			invocationTargetException.printStackTrace(System.out);
			throw new ServletException(invocationTargetException);
		}
	}
}